package com.nimble.smile.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.TextView;
import android.widget.Toast;

import com.nimble.smile.R;
import com.nimble.smile.service.vo.AccountAuthDTO;
import com.nimble.smile.service.OperationCode;
import com.nimble.smile.utils.SharedPreferenceHelper;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by simon on 17-5-4.
 */

public class LoginActivity extends BaseActivity {

//    @Bind(R.id.tab_guest)
//    TextView tabGuest;
//    @Bind(R.id.tab_staff)
//    TextView tabStaff;
    @Bind(R.id.username)
    TextView userName;
    @Bind(R.id.password)
    TextView passwordText;

    private boolean isStaff;

    public static void backWithClearTop(Context context) {
        Intent intent = new Intent(context, LoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
//        switchTab();
    }

//    @OnClick(R.id.tab_guest)
//    void tabGuestClick() {
//        isStaff = false;
//        switchTab();
//    }
//
//    @OnClick(R.id.tab_staff)
//    void setTabStaffClick() {
//        isStaff = true;
//        switchTab();
//    }

    @OnClick(R.id.register_btn)
    void registerBtnClick() {
        startActivity(new Intent(this, RegisterActivity.class));
    }

    @OnClick(R.id.loginBtn)
    void login() {
        String username = userName.getText().toString();
        String password = passwordText.getText().toString();
        if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
            Toast.makeText(this, "用户名密码不能为空", Toast.LENGTH_LONG).show();
        } else {
            showProgressDialog("登录中...");
            AccountAuthDTO account = new AccountAuthDTO();
            account.setMobile(username);
            account.setPassword(password);

            subscribe(getService().auth(account), response -> {
                hideProgressDialog();
                if (response.getOperationCode() == OperationCode.SUCCESS.getValue()) {
                    SharedPreferenceHelper.setSharedPreferenceString("token", response.getBody());
                    SharedPreferenceHelper.setSharedPreferenceString("mobile", username);
                    startActivity(new Intent(this, MainActivity.class));
                    finish();
                } else {
                    showToast(response.getMessage());
                }
            });
        }

    }

//    private void switchTab() {
//        tabGuest.setSelected(isStaff);
//        tabStaff.setSelected(!isStaff);
//    }
}
